plant_dat <- read_dta(str_c(TRADING_DATA_CLEAN, "panel_plant-period-date-bid.dta"))
OUTPUT_dir <- TRADING_FIGS

model_dat <- plant_dat %>%
  filter(
    bid_status != "REJ",
    !is.na(bid_qty),
    bid_day_norm <= .5
  ) %>%
  arrange(gpcb_id, period, date, bid_id)

mod_result <- fixest::feols(ln_bid_price ~ ln_permit_holding_hyp -1 | factor(plant_period_id) ,
                         data = model_dat)

# Plot all bids in first half residualized and with model fit ---------------------------------------------
model_dat %>%
  mutate(old_ln_permit = ln_permit_holding_hyp,
         ln_permit_holding_hyp = 0) %>%
  modelr::add_predictions(mod_result) %>%
  mutate(resid = ln_bid_price-pred,
         beta_line = old_ln_permit*coef(mod_result)[["ln_permit_holding_hyp"]]) %>%
  ggplot(aes(x=old_ln_permit, y=resid)) +
  geom_point(color="dodgerblue2", shape=1)+
  geom_line(aes(y=beta_line), color="orange", size=1.2)+#, linetype=5)+
  theme_classic(base_size=14)+
  scale_y_continuous(name = "Residualized log bid price (INR/kg)") +#, trans = "exp", limits = c(-6,-2.3)) +
  scale_x_continuous(name = "Log plant emissions (kg)") +#, trans = "exp", limits = c(0,9))
  theme(text = element_text(family = "serif"))

ggsave(str_c(OUTPUT_dir, "Figure_6.pdf"),
       height = 5,
       width = 7,
       units = "in")
